/**
 * 用户反馈页面
 * author wim
 */
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_aygsh/http/Api.dart';
import 'package:flutter_aygsh/http/HttpUtil.dart';
import 'package:flutter_aygsh/util/AppSaveInfoUtil.dart';
import 'package:flutter_aygsh/util/LoadingUtil.dart';

import '../../base/Constant.dart';

class FeedBackPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return FeedBackView();
  }
}

class FeedBackView extends State<FeedBackPage> {
  TextEditingController contentControl = new TextEditingController();
  TextEditingController phoneControl = new TextEditingController();
  TextEditingController phoneControl2 = new TextEditingController();
  String token = "";
  @override
  void initState() {
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
    });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);

    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: Text('意见反馈',
            style: TextStyle(
                fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                color: Colors.black)),
        elevation:0.2,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
        actions: <Widget>[
          GestureDetector(
            child: Container(
              alignment: Alignment.center,
              child: Text(
                '提交',
                style: TextStyle(
                    fontSize: ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                    color: Colors.black54),
              ),
              padding: EdgeInsets.fromLTRB(0, 0, 15, 0),
            ),
            onTap: () {
              submitFeed();
            },
          )
        ],
      ),
      body: new SingleChildScrollView(
        child: new Container(
          child: new Column(
            children: <Widget>[
              new Container(
                margin: EdgeInsets.fromLTRB(
                    0.0, ScreenUtil.getInstance().setHeight(DP_20), 0, 10),
                padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                decoration: new BoxDecoration(
                  color: Colors.white,
                ),
                child: TextField(
                  maxLines: 4,
                  minLines: 4,
                  cursorColor: mainColor,
                  keyboardAppearance: Brightness.light,
                  controller: contentControl,
                  decoration: new InputDecoration(
                      hintText: "请输入您遇到的详细问题(必填)",
                      border: InputBorder.none,
                      hintStyle: TextStyle(
                          fontSize: ScreenUtil.getInstance()
                              .setSp(NORMAL_FONT_SIZE))),
                ),
              ),
              new Container(
                width: double.infinity,
                height: ScreenUtil.getInstance().setHeight(DP_80),
                margin: EdgeInsets.fromLTRB(50, 30, 50, 0),
                decoration: new BoxDecoration(
                  color: mainColor,
                    borderRadius: BorderRadius.circular(20.0)),
                padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                child: new FlatButton(
                  child: new Padding(
                    padding: EdgeInsets.all(0.0),
                    child: new Text(
                      "提交",
                      style: new TextStyle(color: Colors.white, fontSize: 16),
                    ),
                  ),
                  onPressed: () {
                    submitFeed();
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }


  void submitFeed() {
    String msgStr = "";
    if (!contentControl.text.isNotEmpty) {
      msgStr = "反馈内容不能为空";
    }
    // if (!phoneControl2.text.isNotEmpty) {
    //   msgStr = "姓名不能为空";
    // }
    if (msgStr != '') {
      showDialog(
          context: context,
          builder: (context) {
            return new AlertDialog(
              title: new Text("提示信息"),
              content: new Text(msgStr),
              actions: <Widget>[
                new FlatButton(
                    child: new Text("确定"),
                    onPressed: () {
                      Navigator.of(context).pop();
                    }),
              ],
            );
          });
    } else {
      showDialog(
          context: context,
          barrierDismissible: false,
          builder: (_) {
            return new NetLoadingDialog(
              loadingText: "提交中...",
              bgColor: Color.fromARGB(255, 0, 0, 0),
              requestCallBack: null,
              outsideDismiss: false,
            );
          });
      register();
    }
  }

  register() async {
    // List<UploadFileInfo> infos = new List();
    // for (int i = 0; i < images.length - 1; i++) {
    //   String path = await images[i].filePath;
    //   var name = path.substring(path.lastIndexOf("/") + 1, path.length);
    //   var suffix = name.substring(name.lastIndexOf(".") + 1, name.length);
    //   infos.add(new UploadFileInfo(new File(path), name,
    //       contentType: ContentType.parse("image/$suffix")));
    // }
    FormData formData = new FormData.fromMap({
      // "phone": phoneControl.text,
      // "cat_id": 1,
      // "linkman": phoneControl2.text,
      "token": token,
      "content": contentControl.text,
      // "img[]": infos
    });
    Response<String> response =
        await HttpUtil().post(Api.URL_ONLINE, data: formData);
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Navigator.of(context).pop(1);
    Fluttertoast.showToast(msg: msg);
    if (code == 1) {
      Navigator.of(context).pop();
    }
    return true;
  }
}
